package com.todod.base.module.base.controller.admin.datadistribute;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import com.todod.base.framework.excel.core.util.ExcelUtils;

import com.todod.base.framework.apilog.core.annotation.ApiAccessLog;
import static com.todod.base.framework.apilog.core.enums.OperateTypeEnum.*;

import com.todod.base.module.base.controller.admin.datadistribute.vo.*;
import com.todod.base.module.base.dal.dataobject.datadistribute.DataDistributeDO;
import com.todod.base.module.base.service.datadistribute.DataDistributeService;

@Tag(name = "管理后台 - 数据分发设置")
@RestController
@RequestMapping("/base/data-distribute")
@Validated
public class DataDistributeController {

    @Resource
    private DataDistributeService dataDistributeService;

    @PostMapping("/create")
    @Operation(summary = "创建数据分发设置")
    public CommonResult<Long> createDataDistribute(@Valid @RequestBody DataDistributeSaveReqVO createReqVO) {
        return success(dataDistributeService.createDataDistribute(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据分发设置")
    public CommonResult<Boolean> updateDataDistribute(@Valid @RequestBody DataDistributeSaveReqVO updateReqVO) {
        dataDistributeService.updateDataDistribute(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据分发设置")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteDataDistribute(@RequestParam("id") Long id) {
        dataDistributeService.deleteDataDistribute(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据分发设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DataDistributeRespVO> getDataDistribute(@RequestParam("id") Long id) {
        DataDistributeDO dataDistribute = dataDistributeService.getDataDistribute(id);
        return success(BeanUtils.toBean(dataDistribute, DataDistributeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据分发设置分页")
    public CommonResult<PageResult<DataDistributeRespVO>> getDataDistributePage(@Valid DataDistributePageReqVO pageReqVO) {
        PageResult<DataDistributeDO> pageResult = dataDistributeService.getDataDistributePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataDistributeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据分发设置 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataDistributeExcel(@Valid DataDistributePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataDistributeDO> list = dataDistributeService.getDataDistributePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据分发设置.xls", "数据", DataDistributeRespVO.class,
                        BeanUtils.toBean(list, DataDistributeRespVO.class));
    }

}